<?php
namespace tool;

class Common
{
    public static function rm($code = 0, $msg = '', $content = '')
    {
        $res = [];
        if (is_array($content) || $content) {
           $res['code'] = $code;
           $res['msg'] = $msg;
           $res['content'] = $content;
           return $res;
        } else {
           $res['code'] = $code;
           $res['msg'] = $msg;
           return $res;
        }
    }

    public static function json($data, $json_option=0)
    {
        header('Content-Type:application/json; charset=utf-8');
        $json = json_encode($data, $json_option);
        exit($json);
    }

    //发送短信验证码
    public static function sendSMS($mobile,$text)
    {
        $ch = curl_init();
        $apikey = "a06c7c993db6aa6f7279e57caeb51b92";
        $data = array('text' => $text, 'apikey' => $apikey, 'mobile' => $mobile);
        curl_setopt($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/single_send.json');   //需要获取的 URL 地址，也可以在curl_init() 初始化会话的时候
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));      //全部数据使用HTTP协议中的 "POST" 操作来发送
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);     //TRUE 将curl_exec()获取的信息以字符串返回，而不是直接输出
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);    //FALSE 禁止 cURL 验证对等证书
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);    //设置为 1 是检查服务器SSL证书中是否存在一个公用名
        $json_data = curl_exec($ch);    //执行会话
        $array = json_decode($json_data, true);
        return $array['code'];
    }

    public static function isMobile($mobile) {
        if (!is_numeric($mobile)) {
            return false;
        }
        return preg_match('#^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$#', $mobile) ? true : false;
    }

    //生成随机字符串
    public static function createStr($length = 32)
    {
        $chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
        $str = '';
        for ( $i = 0; $i < $length; $i++ ) {
            $str .= substr($chars, mt_rand(0, strlen($chars)-1), 1);
        }
        return md5($str);
    }

    //判断变量是否设置并不为空
    public static function checkVariate($variate)
    {
        if (isset($variate) && $variate) {
            return true;
        }
    }
}